package com.zklh.leetcode.top100.problem543;

import com.zklh.leetcode.tree.model.TreeNode;

/**
 * @author Nelson
 * @Description 解法1
 * @create 2023-02-07 17:25
 */
public class Solution1 {

    private int maxDiameter = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        deep(root);
        return maxDiameter;
    }


    private int deep(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDeep = deep(root.left);
        int rightDeep = deep(root.right);

        maxDiameter = Math.max(maxDiameter, leftDeep + rightDeep);

        return 1 + Math.max(leftDeep, rightDeep);
    }

    public static void main(String[] args) {
        Solution1 solution = new Solution1();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        System.out.println(solution.diameterOfBinaryTree(root));
    }
}
